<template>
    <div>
        <h1>类目名:{{ course_name }}</h1>
        <el-button @click="addCategory()">添加列表数据</el-button>
        <!-- 显示题目列表 -->
        <div class="table_container">
            <el-table :data="tableData" border style="width: 90%">
                <el-table-column label="标题名" prop="title" />
                <el-table-column>
                    <template #header>
                        难易程度
                    </template>
                    <template #default="scope">
                        <el-tag :type="isDiffType(scope.row.isdifficulty)">{{ isDiff(scope.row.isdifficulty) }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column>
                    <template #header>
                        是否热门
                    </template>
                    <template #default="scope">
                        <el-tag :type="isHotType(scope.row.ishot)">{{ isHot(scope.row.ishot) }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="描述" prop="description" />
                <el-table-column label="创建时间" prop="create_time" />
                <el-table-column align="right">
                    <template #header>
                        <el-input v-model="search" size="small" placeholder="请输入想要标题名" />
                    </template>
                    <template #default="scope">
                        <el-button size="small" @click="handleAddList(scope.$index, scope.row)">
                            查看内容
                        </el-button>
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
                            编辑
                        </el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>




        <!-- 添加题目 -->
        <el-dialog v-model="dialogFormVisible" title="添加题目" width="500">
            <el-form>
                <el-form-item label="题目名称">
                    <el-input v-model="title" autocomplete="off" />
                </el-form-item>
                <el-form-item label="题目描述">
                    <el-input v-model="description" autocomplete="off" />
                </el-form-item>
                <el-form-item label="难易程度">
                    <el-select v-model="isdifficulty" placeholder="请选择">
                        <el-option v-for="item in diffOptions" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="热门">
                    <el-select v-model="ishot" placeholder="请选择">
                        <el-option v-for="item in hotOptions" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="clickOK">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>

    </div>
</template>




<script setup>
import { ref, computed } from "vue";
import { formatDate } from "@/utils/date.js";
import { $_add_courseslist, $_get_courseslist, $_delete_courseslist } from "@/api/user.js";
import { useRoute,useRouter } from "vue-router";
import { commonMessage } from "@/utils";
const route = useRoute()
const router = useRouter()
const course_id = ref(route.query.course_id)
const course_name = ref(route.query.course_name)
const dialogFormVisible = ref(false)
const search = ref('')
const title = ref('')
const description = ref('')
const isdifficulty = ref('')
const ishot = ref('')
const page = ref(1)
const pageSize = ref(10)
const keyword = ref('')
const tableData = ref([])
const diffOptions = [
    {
        value: 0,
        label: '简单',
    },
    {
        value: 1,
        label: '中等',
    },
    {
        value: 2,
        label: '困难',
    },

]
const hotOptions = [
    {
        value: 0,
        label: '是',
    },
    {
        value: 1,
        label: '否',
    },

]
const addCategory = () => {
    dialogFormVisible.value = true
}
const clickOK = () => {
    dialogFormVisible.value = false
    $_add_courseslist({
        course_id: course_id.value,
        title: title.value,
        description: description.value,
        isdifficulty: isdifficulty.value,
        ishot: ishot.value,
    }).then(res => {
        console.log(res);
        if (res.success) {
            commonMessage(res.msg)
            page.value = 1
            pageSize.value = 10
            getList()
        }
    })


}
const handleEdit = (index, row) => {
    console.log(index, row)
}
const handleDelete = (index, row) => {
    ElMessageBox.confirm(
        '请确认是否删除?',
        'Warning',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            $_delete_courseslist({
                lists_id: row.lists_id
            }).then(res => {
                if (res.success) {
                    commonMessage(res.msg)
                    page.value = 1
                    pageSize.value = 10
                    getList()
                }
            })
        })
}
const handleAddList = (index, row) => {
    console.log(row);
    router.push({
        path: '/text',
        query: {
            course_id: course_id.value,
            course_name: course_name.value,
            lists_id: row.lists_id
        }
    })
}
const getList = () => {
    $_get_courseslist({
        course_id: course_id.value,
        page: page.value,
        pageSize: pageSize.value,
        keyword: keyword.value
    }).then(res => {
        for (const iterator of res.result) {
            iterator.create_time = formatDate(iterator.create_time)
            iterator.description = iterator.description || '~'
        }
        tableData.value = res.result
    })
}
getList()

const isDiff = (i) => {
    if (i === '0') {
        return '简单'
    } else if (i === '1') {
        return '中等'
    } else {
        return '困难'
    }
}
const isDiffType = (i) => {
    if (i === '0') {
        return 'success'
    } else if (i === '1') {
        return 'warning'
    } else {
        return 'danger'
    }
}

const isHot = (i) => {
    if (i === 0) {
        return '是'
    } else if (i === 1) {
        return '否'
    }
}
const isHotType = (i) => {
    if (i === 0) {
        return 'danger'
    } else if (i === 1) {
        return 'success'
    }
}


</script>

<style lang="scss" scoped>
.table_container {
    width: 100%;
    display: flex;
    justify-content: center;
}
</style>